/**
 * Created with WebStorm.
 * @Author: panda
 * @Date: 2022-05-18 09:21
 * @Description: 文章接口
 */

import { createRequestClient } from './axios/request';
import { APISchema } from "@/api/axios/type";
import localCache from "@/utils/localCache";

interface Article {
  id: number,
  title: string,
  content: string,
  thumbnail: string,
  views: number,
  categoryId: string,
  categoryName: string,
  createTime: string,
  likes: number,
}

//文章请求响应类型
export interface ArticleSchema extends APISchema {
  getAllArticles: {
    request: {
      pageNum: number, pageSize: number, categoryId?: number, keyword?: string
    };
    response: {
      code: number,
      data: { rows: Array<Article>, total: number }
      msg: string
    };
  };
  postArticle: {
    request: {
      title: string,
      summary: string,
      content: string,
      categoryId: string,
      thumbnail: string,
      copyright: string,
    };
    response: {
      code: number,
      msg: string
    };
  };
  hotArticleList: {
    request: {};
    response: {
      code: number,
      data: {},
      msg: string
    };
  };
  getArticleDetail: {
    request: { id: number };
    response: {
      code: number,
      data: {
        categoryId: string,
        categoryName: string,
        createTime: string,
        id: string,
        content: string,
        likes: number,
        summary: string,
        thumbnail: string,
        title: string,
        views: string
        copyright: string
      },
      msg: string
    };
  };
  getSelfArticles: {
    request: {
      pageNum: number, pageSize: number
    };
    response: {
      code: number,
      data: Array<Article>
      msg: string
    };
  }
  modifyArticle: {
    request: {
      id: number
      title: string,
      summary: string,
      content: string,
      categoryId: string,
      thumbnail: string,
      copyright: string,
    };
    response: {
      code: number,
      msg: string
    };
  }
  deleteArticle: {
    request: {
      id: number
    };
    response: {
      code: number,
      msg: string
    };
  }
}

//构造文章api
export const articleApi = createRequestClient<ArticleSchema>({
  baseURL: process.env.NODE_ENV === 'development'
    ? process.env.VUE_APP_API_HOST + ''
    : '',
  headers: { 'Content-Type': 'application/json' },
  apis: {
    postArticle: {
      method: 'POST',
      path: 'POST /articles',
      headers: { 'token': localCache.getCache('token') }
    },

    getAllArticles: {
      method: 'GET',
      path: '/articles',
      headers: {}
    },

    getSelfArticles: 'POST /article/getSelfArticles',
    hotArticleList: 'GET /article/hotArticleList',
    getArticleDetail: 'GET /articles/:id',
    modifyArticle: {
      method: 'PUT',
      path: 'PUT /articles/:id',
      headers: { token: localCache.getCache('token') }
    },
    deleteArticle: {
      method: 'DELETE',
      path: 'DELETE /articles/:id',
      headers: { token: localCache.getCache('token') }
    }
  }
});
